package com.steven.ourcontest.team.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.steven.ourcontest.base.BaseViewModel
import com.steven.ourcontest.collect.api.CollectApi
import com.steven.ourcontest.team.api.TeamApi
import com.steven.ourcontest.team.bean.TeamDetail
import com.steven.ourcontest.team.helper.TeamController
import com.steven.ourcontest.utils.plusAssign

class TeamViewModel : BaseViewModel() {
    private val _teamResponse = MutableLiveData<TeamDetail>()
    val teamResponse: LiveData<TeamDetail>
        get() = _teamResponse


    fun queryTeamDetail(teamId: Long) {
        disposed += TeamApi.instance.queryTeamDetail(teamId)
            .subscribe({
                _teamResponse.value = it
            }, {
                it.printStackTrace()
            })
    }

    fun updateTeamStatus(teamId: Long, status: Boolean, error: (() -> Unit)) {
        disposed += TeamApi.instance.updateTeamStatus(teamId, status)
            .subscribe({
                TeamController.setTeamStatus(teamId, status)
            }, {
                it.printStackTrace()
                error.invoke()
            })
    }

    fun addTeamPageViews(teamId: Long) {
        disposed += TeamApi.instance.addTeamPageViews(teamId)
            .subscribe({
            }, {
                it.printStackTrace()
            })
    }

    fun addTeamCollection(teamId: Long, success: () -> Unit) {
        disposed += CollectApi.instance.addTeamCollection(teamId)
            .subscribe({
                success.invoke()
            }, {
                it.printStackTrace()
            })
    }

    private var deleting = false
    fun deleteTeamCollection(teamId: Long, success: () -> Unit) {
        if (deleting)
            return
        deleting = true
        disposed += CollectApi.instance.deleteTeamCollection(teamId)
            .subscribe({
                deleting = false
                success.invoke()
            }, {
                deleting = false
                it.printStackTrace()
            })
    }
}